#!/bin/sh
###############################################################################
##  Author    : xuezhoyi
##  Name      : edw_parm_refresh.sh
##  Functions : Refresh parm file
##  Purpose   : Refresh parm file
##  Revisions or Comments
##  VER        DATE        AUTHOR           DESCRIPTION
##---------  ----------  ---------------  ------------------------------------ 
##  1.0      2017-05-22  xuezhouyi        1. CREATED THIS SHELL.
###############################################################################

. ${0%`basename ${0}`}edw_parm.sh
. ${0%`basename ${0}`}edw_func.sh

function USAGE(){
    echo -e "\nHow to use this shell script!"
    echo -e '\n${V_SHELL_HOME}/edw_parm_refresh.sh 20170101 5Q01'
}

# Check variable
if [[ -z ${1} || -z ${2} ]];then
    USAGE
    exit 1
fi

# Define basic parm
V_SCRIPT_NAME=$(basename ${0} | cut -d'.' -f1)
V_OUT_LOGS="${V_SHELL_LOGS}/${V_SCRIPT_NAME}_${2}_${V_TIME_STAMP}_$$.log"

# Define the output flow
exec 4>&1               # screen output
exec 3>>${V_OUT_LOGS}   # script output
exec 2>&3               # error output
exec 1>&3               # standard output
echo -e "[Begin: $(date +%F) $(date +%T)]"

# Get parm
V_DATA_DATE_1331=${1}
V_DATA_SRC_ORG=${2}
V_DATA_DATE="${V_DATA_DATE_1331:0:4}1231"
# create the folder if not exists
if [[ ! -d "${V_SHELL_PARM}/${V_DATA_SRC_ORG}" ]];then
    mkdir -p "${V_SHELL_PARM}/${V_DATA_SRC_ORG}"
fi

# Check the batch
while true;do
    V_QUERY="SELECT COALESCE(${V_DATA_SRC_ORG},'X') FROM ETL.EDW_BATCH_CONTROL WHERE DATA_DATE = '${V_DATA_DATE}';"
    V_BATCH_IND=$(${V_RUN_MYSQL} -q "${V_QUERY}")
    if [[ ${V_BATCH_IND} == 'Y' || ${V_BATCH_IND} == 'N' ]];then
        break
    fi
    # sleep
    echo -e "WARNING:Waiting for data status[${V_BATCH_IND}] after $((${V_WAIT_TIME}/60)) mins..."
    sleep ${V_WAIT_TIME}
done

# Translate paramater and output
echo "BATCH_IND=${V_BATCH_IND}"                                             >  "${V_SHELL_PARM}/${V_DATA_SRC_ORG}/edw_parm.txt"
echo "#V_DATA_DATE#=${V_DATA_DATE_1331}"                                    >> "${V_SHELL_PARM}/${V_DATA_SRC_ORG}/edw_parm.txt"
echo "#V_DATA_SRC_ORG#=${V_DATA_SRC_ORG}"                                   >> "${V_SHELL_PARM}/${V_DATA_SRC_ORG}/edw_parm.txt"
echo "#V_CURR_DATE#=${V_DATA_DATE_1331}"                                    >> "${V_SHELL_PARM}/${V_DATA_SRC_ORG}/edw_parm.txt"
#echo "#V_PREV_DATE#=$(GET_PREV_DATE ${V_DATA_DATE})"                        >> "${V_SHELL_PARM}/${V_DATA_SRC_ORG}/edw_parm.txt"
#echo "#V_NEXT_DATE#=$(GET_NEXT_DATE ${V_DATA_DATE})"                        >> "${V_SHELL_PARM}/${V_DATA_SRC_ORG}/edw_parm.txt"
echo "#V_LAST_MONTH_DATE#=${V_DATA_DATE}"                               >> "${V_SHELL_PARM}/${V_DATA_SRC_ORG}/edw_parm.txt"
echo "#V_LAST_MONTH_END_DATE#=${V_DATA_DATE}"                               >> "${V_SHELL_PARM}/${V_DATA_SRC_ORG}/edw_parm.txt"
echo "#V_LAST_YEAR_DATE#=$(GET_PREV_YEAR ${V_DATA_DATE})1331"               >> "${V_SHELL_PARM}/${V_DATA_SRC_ORG}/edw_parm.txt"
echo "#V_NEXT_ONE_MONTH_END_DATE#=$(GET_N_MONTH_END_DATE ${V_DATA_DATE} -1)"    >> "${V_SHELL_PARM}/${V_DATA_SRC_ORG}/edw_parm.txt"
echo "#V_NEXT_TWO_MONTH_END_DATE#=$(GET_N_MONTH_END_DATE ${V_DATA_DATE} -2)"    >> "${V_SHELL_PARM}/${V_DATA_SRC_ORG}/edw_parm.txt"
echo "#V_NEXT_THREE_MONTH_END_DATE#=$(GET_N_MONTH_END_DATE ${V_DATA_DATE} -3)"    >> "${V_SHELL_PARM}/${V_DATA_SRC_ORG}/edw_parm.txt"
echo "#V_NEXT_FOUR_MONTH_END_DATE#=$(GET_N_MONTH_END_DATE ${V_DATA_DATE} -4)"    >> "${V_SHELL_PARM}/${V_DATA_SRC_ORG}/edw_parm.txt"
echo "#V_NEXT_FIVE_MONTH_END_DATE#=$(GET_N_MONTH_END_DATE ${V_DATA_DATE} -5)"    >> "${V_SHELL_PARM}/${V_DATA_SRC_ORG}/edw_parm.txt"
echo "#V_NEXT_SIX_MONTH_END_DATE#=$(GET_N_MONTH_END_DATE ${V_DATA_DATE} -6)"    >> "${V_SHELL_PARM}/${V_DATA_SRC_ORG}/edw_parm.txt"
#echo "#V_CURR_TENS#=$(GET_CURR_TENS ${V_DATA_DATE})"                        >> "${V_SHELL_PARM}/${V_DATA_SRC_ORG}/edw_parm.txt"
#echo "#V_PREV_TENS#=$(GET_PREV_TENS ${V_DATA_DATE})"                        >> "${V_SHELL_PARM}/${V_DATA_SRC_ORG}/edw_parm.txt"
#echo "#V_NEXT_TENS#=$(GET_NEXT_TENS ${V_DATA_DATE})"                        >> "${V_SHELL_PARM}/${V_DATA_SRC_ORG}/edw_parm.txt"
echo "#V_LAST_YEAR_MONTH#=$(GET_PREV_YEAR ${V_DATA_DATE})13"                >> "${V_SHELL_PARM}/${V_DATA_SRC_ORG}/edw_parm.txt"
echo "#V_LAST_TWO_MONTH#=$(GET_CURR_YEAR ${V_DATA_DATE})12"                 >> "${V_SHELL_PARM}/${V_DATA_SRC_ORG}/edw_parm.txt"
echo "#V_LAST_THREE_MONTH#=$(GET_LAST_N_MONTH ${V_DATA_DATE} 2)"            >> "${V_SHELL_PARM}/${V_DATA_SRC_ORG}/edw_parm.txt"
echo "#V_LAST_FOUR_MONTH#=$(GET_LAST_N_MONTH ${V_DATA_DATE} 3)"             >> "${V_SHELL_PARM}/${V_DATA_SRC_ORG}/edw_parm.txt"
echo "#V_LAST_FIVE_MONTH#=$(GET_LAST_N_MONTH ${V_DATA_DATE} 4)"             >> "${V_SHELL_PARM}/${V_DATA_SRC_ORG}/edw_parm.txt"
#echo "#V_LAST_THIRTEEN_MONTH#=$(GET_LAST_N_MONTH ${V_DATA_DATE} 13)"        >> "${V_SHELL_PARM}/${V_DATA_SRC_ORG}/edw_parm.txt"
#echo "#V_LAST_FOURTEEN_MONTH#=$(GET_LAST_N_MONTH ${V_DATA_DATE} 14)"        >> "${V_SHELL_PARM}/${V_DATA_SRC_ORG}/edw_parm.txt"
#echo "#V_LAST_FIFTEEN_MONTH#=$(GET_LAST_N_MONTH ${V_DATA_DATE} 15)"         >> "${V_SHELL_PARM}/${V_DATA_SRC_ORG}/edw_parm.txt"
#echo "#V_LAST_SIXTEEN_MONTH#=$(GET_LAST_N_MONTH ${V_DATA_DATE} 16)"         >> "${V_SHELL_PARM}/${V_DATA_SRC_ORG}/edw_parm.txt"
#echo "#V_LAST_SEVENTEEN_MONTH#=$(GET_LAST_N_MONTH ${V_DATA_DATE} 17)"       >> "${V_SHELL_PARM}/${V_DATA_SRC_ORG}/edw_parm.txt"
echo "#V_CURR_MONTH#=$(GET_CURR_YEAR ${V_DATA_DATE})13"                      >> "${V_SHELL_PARM}/${V_DATA_SRC_ORG}/edw_parm.txt"
echo "#V_PREV_MONTH#=$(GET_CURR_YEAR ${V_DATA_DATE})12"                      >> "${V_SHELL_PARM}/${V_DATA_SRC_ORG}/edw_parm.txt"
echo "#V_NEXT_MONTH#=$(GET_NEXT_YEAR ${V_DATA_DATE})01"                      >> "${V_SHELL_PARM}/${V_DATA_SRC_ORG}/edw_parm.txt"
echo "#V_BEGIN_MONTH#=${V_DATA_DATE:0:4}"                                       >> "${V_SHELL_PARM}/${V_DATA_SRC_ORG}/edw_parm.txt"
echo "#V_LAST_MONTH#=$(GET_LAST_YEAR_DATE ${V_DATA_DATE})"                      >> "${V_SHELL_PARM}/${V_DATA_SRC_ORG}/edw_parm.txt"
#echo "#V_CURR_QUARTER#=$(GET_CURR_QUARTER ${V_DATA_DATE})"                  >> "${V_SHELL_PARM}/${V_DATA_SRC_ORG}/edw_parm.txt"
#echo "#V_PREV_QUARTER#=$(GET_PREV_QUARTER ${V_DATA_DATE})"                  >> "${V_SHELL_PARM}/${V_DATA_SRC_ORG}/edw_parm.txt"
#echo "#V_NEXT_QUARTER#=$(GET_NEXT_QUARTER ${V_DATA_DATE})"                  >> "${V_SHELL_PARM}/${V_DATA_SRC_ORG}/edw_parm.txt"
echo "#V_CURR_YEAR#=$(GET_NEXT_YEAR ${V_DATA_DATE})"                        >> "${V_SHELL_PARM}/${V_DATA_SRC_ORG}/edw_parm.txt"
echo "#V_PREV_YEAR#=$(GET_CURR_YEAR ${V_DATA_DATE})"                        >> "${V_SHELL_PARM}/${V_DATA_SRC_ORG}/edw_parm.txt"
echo "#V_PREV_YEAR_END_DATE#=${V_DATA_DATE}"                                >> "${V_SHELL_PARM}/${V_DATA_SRC_ORG}/edw_parm.txt"
echo "#V_NEXT_YEAR#=$(GET_NEXT_YEAR $(GET_NEXT_YEAR ${V_DATA_DATE}))"       >> "${V_SHELL_PARM}/${V_DATA_SRC_ORG}/edw_parm.txt"
echo "#V_MONTH_FIRST_DATE#=${V_DATA_DATE_1331}"                             >> "${V_SHELL_PARM}/${V_DATA_SRC_ORG}/edw_parm.txt"
echo "#V_MONTH_FINAL_DATE#=${V_DATA_DATE_1331}"                             >> "${V_SHELL_PARM}/${V_DATA_SRC_ORG}/edw_parm.txt"
echo "#V_CURR_YEAR_START_DATE#=${V_DATA_DATE_1331}"          >> "${V_SHELL_PARM}/${V_DATA_SRC_ORG}/edw_parm.txt"

if [[ $? -ne 0 ]];then
    echo -e "\nCan not create the parm file[${V_SHELL_PARM}/${V_DATA_SRC_ORG}/edw_parm.txt]."
    exit 1
else
    echo -e "\nOK to create parm file: ${V_SHELL_PARM}/${V_DATA_SRC_ORG}/edw_parm.txt"
fi

echo -e "\n[End: $(date +%F) $(date +%T)]"
exit 0
